import { getListApi } from "@/api/informCenter/notificationMessage";
import dayjs from "dayjs";
import { Ref } from "vue";

export default function OperationHook(state: { loading: Ref<boolean>; searchForm: NotificationMessage.SearchForm; tableData: Ref<NotificationMessage.ListRows[]> }) {
  const { loading, tableData, searchForm } = state;
  const getData = () => {
    loading.value = true;
    const params = {
      ...searchForm,
      noticeTime: searchForm.noticeTime.length
        ? [dayjs(searchForm.noticeTime[0]).startOf("day").format("YYYY-MM-DD HH:mm:ss"), dayjs(searchForm.noticeTime[1]).endOf("day").format("YYYY-MM-DD HH:mm:ss")]
        : []
    };
    return getListApi(params)
      .then((res: any) => {
        const { code, data } = res;
        if (code === 200) {
          tableData.value = data.records || [];
          searchForm.beforePageNum = searchForm.pageNum;
        }
      })
      .finally(() => (loading.value = false));
  };
  const searchHandle = () => {
    searchForm.pageNum = 1;
    getData();
  };

  return {
    searchHandle,
    getData
  };
}
